<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="农机手姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agricultureName">
              <a-input v-model="model.agricultureName" placeholder="请输入农机手姓名"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="农机手描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agricultureDescribe">
              <a-textarea v-model="model.agricultureDescribe" rows="4" placeholder="请输入农机手描述" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="描述图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="describeImg">
              <j-image-upload isMultiple v-model="model.describeImg" :number="9"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">一次上传可选择多张图片！请一次性上传完所需图片！</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="封面图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coverImg">
              <j-image-upload isMultiple v-model="model.coverImg" :number="1"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">只能上传一张图片</div>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="驾照类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="drivingType">
              <!-- <a-input v-model="model.drivingType" placeholder="请输入驾照类型"></a-input> -->
              <j-dict-select-tag type="list" v-model="model.drivingType" dictCode="agriculture_license_type"
                placeholder="请选择驾照类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="擅长农机械" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="genius">
              <!-- <a-input v-model="model.genius" placeholder="请输入擅长农机械"></a-input> -->
              <j-dict-select-tag type="list" v-model="model.genius" dictCode="agriculture_machinery_type"
                placeholder="请选择擅长农机械" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="身份证号码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="card">
              <a-input v-model="model.card" placeholder="请输入身份证号码"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <!-- <a-input v-model="model.sex" placeholder="请输入性别"></a-input> -->
              <a-radio-group v-model="model.sex">
                <a-radio value="0">男</a-radio>
                <a-radio value="1">女</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item label="工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hours">
              <a-input v-model="model.hours" placeholder="请输入工时"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="驾驶许可证(图片)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="drivingImg">
              <j-image-upload isMultiple v-model="model.drivingImg" :number="1"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">只能上传一张图片</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详情介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="describes">
              <j-editor v-model="model.describes"></j-editor>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <!-- <a-input v-model="model.type" placeholder="请输入状态"></a-input> -->
              <a-radio-group v-model="model.type" placeholder="请选择状态">
                <a-radio value="1">审核中</a-radio>
                <a-radio value="2">未通过</a-radio>
                <a-radio value="3">通过</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MachineAgricultureMachinistForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        // phone: [{ required: true, pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!' }],
        agricultureName: [{ required: true, message: '请输入农机手姓名' }],
        describeImg: [{ required: true, message: '请上传描述图片' }],
        coverImg: [{ required: true, message: '请上传封面图' }],
        drivingType: [{ required: true, message: '请选择驾照类型' }],
        genius: [{ required: true, message: '请选择擅长农机械' }],
        // card: [{ required: true, pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}([0-9]|X)$/, message: '请输入正确的身份证号码！' }],
        sex: [{ required: true, message: '请选择性别' }],
        // hours: [{ required: true, message: '请输入工时' }],
        drivingImg: [{ required: true, message: '请上传驾驶许可证' }],
        describes: [{ required: true, message: '请输入详情介绍' }]
      },
      url: {
        add: "/agriculture/machineAgricultureMachinist/add",
        edit: "/agriculture/machineAgricultureMachinist/edit",
        queryById: "/agriculture/machineAgricultureMachinist/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  },
  // watch: {
  //   'model.describeImg'(newVal, oldVal) {
  //     console.log("现在", newVal);
  //     console.log("过去", oldVal);
  //   }
  // }
}
</script>